package com.sitech.ibnms.c3p.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liujhc
 */
public class SysDict implements Serializable {

    private String dictId;
    private String dictName;
    private String itemsSql;
    private List<SysDictItem> items;
    private Map<String, SysDictItem> itemsMap;

    public SysDict() {
    }

    public SysDict(String dictId, String dictName, List<SysDictItem> items) {
        this.dictId = dictId;
        this.dictName = dictName;
        this.items = items;
    }

    /**
     * 通过字典元素值获取元素名称
     *
     * @param itemValue
     * @return
     */
    public String getItemName(String itemValue) {
        SysDictItem dictItem = getItemsMap().get(itemValue);
        return dictItem == null ? null : dictItem.getItemName();
    }

    /**
     * 获取字典分类的ID
     *
     * @return
     */
    public String getDictId() {
        return dictId;
    }

    public void setDictId(String dictId) {
        this.dictId = dictId;
    }

    /**
     * 获取字典分类名称
     *
     * @return
     */
    public String getDictName() {
        return dictName;
    }

    /**
     * 用于获取数据字典项的SQL
     *
     * @return
     */
    public String getItemsSql() {
        return itemsSql;
    }

    public void setItemsSql(String itemsSql) {
        this.itemsSql = itemsSql;
    }

    /**
     * 获取字典所对应的所有元素列表
     *
     * @return
     */
    public List<SysDictItem> getItems() {
        if (items == null) {
            items = new ArrayList();
        }
        return items;
    }

    public Map<String, SysDictItem> getItemsMap() {
        if (itemsMap == null) {
            itemsMap = new LinkedHashMap<String, SysDictItem>(getItems().size());
            for (SysDictItem item : items) {
                itemsMap.put(item.getItemValue(), item);
            }
        }
        return itemsMap;
    }

    @Override
    public String toString() {
        return "SysDict{" +
                "dictId='" + dictId + '\'' +
                ", dictName='" + dictName + '\'' +
                ", itemsSql='" + itemsSql + '\'' +
                ", items=" + items +
                '}';
    }
}
